package com.qf.administration.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.administration.entity.CarManage;
import com.qf.administration.entity.DriverManage;
import com.qf.administration.mapper.CarManageMapper;
import com.qf.administration.mapper.DriverManageMapper;
import com.qf.administration.mapper.DriverMapper;
import com.qf.administration.qo.DriverManageQO;
import com.qf.administration.service.DriverManageService;
import com.qf.administration.utils.CommonBeanUtils;
import com.qf.administration.vo.DriverManageVO;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Title: DriverManageServiceImpl
 * @Description:
 * @author:ZWX
 * @date: 2023/5/24 17:49
 */
@Service
public class DriverManageServiceImpl implements DriverManageService {
    @Resource
    private DriverManageMapper driverManageMapper;
    @Resource
    private DriverMapper driverMapper;
    @Resource
    private CarManageMapper carManageMapper;

    @Override
    @Transactional
    public RespResult<String> addDriver(DriverManageQO driverManageQO) {
        DriverManage driverManage = driverManageMapper.selectById(driverManageQO.getUserId());
        DriverManage driverManage2 = driverManageMapper.selectById(driverManageQO.getCarId());
        if (ObjectUtils.isEmpty(driverManage) && ObjectUtils.isEmpty(driverManage2)) {
            int i = driverManageMapper.insertById(driverManageQO.getUserId(), driverManageQO.getCarId()[0]);
            int i2 = driverManageMapper.insertById(driverManageQO.getUserId(), driverManageQO.getCarId()[1]);
            if (i == 1 && i2 == 1) {
                DriverManage driverManage1 = new DriverManage();
                BeanUtils.copyProperties(driverManageQO, driverManage1);
                int insert = driverManageMapper.insert(driverManage1);
                int i1 = carManageMapper.updateCarStatus2(driverManageQO.getCarId());
                if (insert == 1 && i1 == 1) {
                    return RespResult.success("添加成功");
                } else {
                    throw new ServiceException(ResultCode.INSERT_ERROR);
                }
            } else {
                throw new ServiceException(ResultCode.INSERT_ERROR);
            }
        } else {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }

    @Override
    public RespResult<String> deleteDriver(int[] ids) {
        int i = driverManageMapper.deleteById(ids);
        if (i > 1) {
            if (i == ids.length) {
                return RespResult.success("删除成功");
            } else {
                return RespResult.success("删除部分数据");
            }
        } else {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }

    @Override
    @Transactional
    public RespResult<String> deleteOneDriver(int vehileId) {
        int userId = driverManageMapper.selectUserId(vehileId);
        int i = driverManageMapper.deleteOneById(userId);
        if (i > 1) {
            int i1 = driverMapper.deleteOneById(userId);
            List<Integer> carIds = driverManageMapper.selectCarId(userId);
            int[] car = new int[carIds.size()];
            for (int j = 0; j < carIds.size(); j++) {
                car[j] = carIds.get(j);
            }
            int i2 = carManageMapper.updateCarStatus(car);
            if (i1 == 1 && i2 == carIds.size()) {
                return RespResult.success("删除成功");
            } else {
                throw new ServiceException(ResultCode.DELETE_ERROR);
            }
        } else {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }

    @Override
    public RespResult<String> updateDriver(DriverManageQO driverManageQO) {
        DriverManage driverManage1 = driverManageMapper.selectById(driverManageQO.getUserId());
        DriverManage driverManage2 = driverManageMapper.selectById(driverManageQO.getCarId());
        if (!ObjectUtils.isEmpty(driverManage1) && !ObjectUtils.isEmpty(driverManage2)) {
            DriverManage driverManage = new DriverManage();
            BeanUtils.copyProperties(driverManageQO, driverManage);
            int i = driverManageMapper.updateById(driverManage);
            if (i > 1) {
                CarManage carManage = new CarManage();
                BeanUtils.copyProperties(driverManageQO, carManage);
                int i1 = carManageMapper.updateById(carManage);
                if (i > 1) {
                    return RespResult.success("修改成功");
                } else {
                    throw new ServiceException(ResultCode.UPDATE_ERROR);
                }
            } else {
                throw new ServiceException(ResultCode.UPDATE_ERROR);
            }
        } else {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
    }

    @Override
    public PageInfo<DriverManageVO> queryDriver(int page, int size, String workType, String username, String sex, String driverNumber, String busNumber) {
        PageInfo<DriverManage> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> driverManageMapper.selectList(workType, username, sex, driverNumber, busNumber));
        if (pageInfo.getTotal() == 0) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return CommonBeanUtils.copyPageInfo(pageInfo, new PageInfo<>(), DriverManageVO::new);
    }

    @Override
    public DriverManageVO queryDriverOne(int id) {
        DriverManage driverManage = driverManageMapper.selectById(id);
        if (ObjectUtils.isEmpty(driverManage)) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        } else {
            DriverManageVO driverManageVO = new DriverManageVO();
            BeanUtils.copyProperties(driverManage, driverManageVO);
            return driverManageVO;
        }

    }
}
